package com.wxg.tools.cmd;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author wxg
 *
 */
public class CmdRuntimeExec {

	private Process process = null;
	
	private Runtime currentRuntime = null;
	
	private String javaHome = null;
	
	public CmdRuntimeExec(){
		currentRuntime = Runtime.getRuntime();
		javaHome = System.getenv("JAVA_HOME");
	}

	public Process getProcess() {
		return process;
	}
	
	public void setJavaHome(String javaHome) {
		this.javaHome = javaHome;
	}

	/**
	 * 
	 * @param commands
	 * @param dir
	 * @return
	 */
	public Process runtimeExec(List<String> commands, String dir){
		String[] emptyarr = new String[]{};
		return runtimeExec(commands.toArray(emptyarr), dir);
	}
	
	/**
	 * 
	 * @param commands
	 * @param dir
	 * @return
	 */
	public Process runtimeExec(String[] commands, String dir){
		String[] emptyarr = new String[]{};
		List<String> env = new ArrayList<String>();
		env.add("JAVA_HOME=" + javaHome);
		//System.getenv("Path"): Path,path都可以。
		env.add("PATH=" + System.getenv("Path"));
		
		return runtimeExec2(commands, env.toArray(emptyarr), new File(dir));
	}
	
	/**
	 * 
	 * @param commands
	 * @param env
	 * @param dir
	 * @return
	 */
	public Process runtimeExec(List<String> commands, List<String> env, File dir){
		String[] emptyarr = new String[]{};
		return runtimeExec2(commands.toArray(emptyarr), env.toArray(emptyarr), dir);
	}
	
	/**
	 * `runtimeExec`方法的版本2 . 将`runtimeExec` deprecated. 
	 * `runtimeExec2`与`runtimeExec`相比， 区别在于使用了`SequenceInputStream`
	 * 
	 * 
	 * @param commands
	 * @param env
	 * @param dir
	 * @return
	 */
	public Process runtimeExec2(String[] commands, String[] env, File dir){
		BufferedReader br = null;
		try{
			process = currentRuntime.exec(commands, env, dir);
			InputStream in = process.getInputStream();
			InputStream errin = process.getErrorStream();
			
			/**
			 * SequenceInputStream
			 * 这里这样用， 是否正确呢？ 
			 */
			SequenceInputStream sis = new SequenceInputStream(in, errin);
			br = new BufferedReader(new InputStreamReader(sis));
			String line = null;
			while((line=br.readLine()) != null){
				System.out.println( line );
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try {
				if(null != br){br.close();}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return process;
	}
	
	/**
	 * 
	 * @param commands
	 * @param env
	 * @param dir
	 * @return
	 */
	@Deprecated
	public Process runtimeExec(String[] commands, String[] env, File dir){
		BufferedReader br = null;
		BufferedReader errbr = null;
		try{
			process = currentRuntime.exec(commands, env, dir);
			InputStream in = process.getInputStream();
			InputStream errin = process.getErrorStream();
			br = new BufferedReader(new InputStreamReader(in));
			errbr = new BufferedReader(new InputStreamReader(errin));
			/**
			 * 把`line`,`errline`分开打印似乎不合理！
			 */
			String line = null, errline = null;
			while((line=br.readLine()) != null){
				System.out.println( line );
			}
			while((errline=errbr.readLine()) != null){
				System.out.println( errline );
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try {
				if(null != br){br.close();}
				if(null != errbr){errbr.close();}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return process;
	}
	
}
